package com.wuye.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.wuye.common.util.CommonUtil;
import com.wuye.common.util.bean.EntityCopyUtil;
import com.wuye.common.vo.ZTreeInfo;
import com.wuye.constants.BaseConstants;
import com.wuye.entity.Area;
import com.wuye.entity.Organization;
import com.wuye.services.AreaServiceManager;

public class AreaAction extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(AreaAction.class);
	@Autowired
	private AreaServiceManager areaServiceManager;
	@Autowired
	private Area area;
	
	private int upAreaId;
	private String areaListStr;
	private String pzJson;
	
	@Setter
	@Getter
	private String inParma;
	@Setter
	@Getter
	private String rtnMsg;
	
	public String queryAreas()
	{
		List<Area> areaList = areaServiceManager.getAreas(upAreaId);
		JSONArray jsarr = new JSONArray();
		
		if(areaList != null && areaList.size() > 0)
		{
			for (Area area : areaList)
			{
				JSONObject jsobj = EntityCopyUtil.getJSONObject(area,null);
				jsarr.add(jsobj);
			}
		}
		
		log.info("return areaJson:" +jsarr);
		areaListStr = jsarr.toString();
		
		return "success";
	}
	
	public int getUpAreaId() {
		return upAreaId;
	}

	public void setUpAreaId(int upAreaId) {
		this.upAreaId = upAreaId;
	}

	public String getAreaListStr() {
		return areaListStr;
	}

	public void setAreaListStr(String areaListStr) {
		this.areaListStr = areaListStr;
	}

	public String getPzJson() {
		return pzJson;
	}

	public void setPzJson(String pzJson) {
		this.pzJson = pzJson;
	}
	
	public String getAreaInfos()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try 
		{
			int openAreaId = Integer.parseInt(jsonObj.getString("id"));
			
			String openAreaIds = areaServiceManager.getParents(openAreaId);
			List<Integer> openIds = CommonUtil.toIntegerList(Arrays.asList(openAreaIds.split(",")));
			
			jsonObj.clear();
			jsonObj.put("found", 1);
			
			List <ZTreeInfo> areaInfos = areaServiceManager.getAreaInfos(openIds);		
			
			if (areaInfos != null && areaInfos.size() > 0)
			{
				jsonObj.put("data", JSONArray.fromObject(areaInfos).toString());				
			}
			else
			{
				jsonObj.put("found", 0);
			}
		} 
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("found", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String addArea()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int upAreaId = Integer.parseInt(jsonObj.getString("pId"));
			String areaName = jsonObj.getString("areaName");
			
			Area upArea = areaServiceManager.getArea(upAreaId);
			
			this.area.setAreaName(areaName);
			this.area.setAreaType(upArea.getAreaType());
			this.area.setUpAreaId(upAreaId);
			this.area.setStatusCd(BaseConstants.STATUS_VALID);
			this.area.setStatusDate(new Date());
			this.area.setUpdateDate(new Date());
			this.area.setCreateDate(new Date());
			
			areaServiceManager.addArea(area);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String updateArea()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int areaId = Integer.parseInt(jsonObj.getString("id"));
			String areaName = jsonObj.getString("areaName");
			
			this.area = areaServiceManager.getArea(areaId);
			
			this.area.setAreaName(areaName);
			this.area.setUpdateDate(new Date());
			
			areaServiceManager.updateArea(area);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
	public String delArea()
	{
		JSONObject jsonObj = JSONObject.fromObject(inParma);
		
		try
		{
			int areaId = Integer.parseInt(jsonObj.getString("id"));
			
			areaServiceManager.delArea(areaId);
			
			jsonObj.clear();
			jsonObj.put("ok", 1);
			
		}
		catch (Exception e) 
		{
			jsonObj.clear();
			jsonObj.put("ok", 0);
			e.printStackTrace();
		}
		finally
		{
			rtnMsg = jsonObj.toString();
		}
		
		log.debug(rtnMsg);
		
		return SUCCESS;
	}
	
}
